package TrafficSim;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.Vector;

import javax.vecmath.Vector2d;

public class Street {

//	private int lanes;
	private int speedLimit;
	private Point start;
	private Point end;
	private String id;
	private Vector2d direction;
	private Vector<Vehicle> vehiclesOnStreet;
	private boolean oneWay = true;
	private Point2D TrafficlightPosition;
	private TrafficLight tl;
	private int priority;

	public Street(int j, String id) {	//might want to add the Street name?
//		this.lanes = i;
		this.speedLimit = j;
		this.id = id;
		this.priority = this.speedLimit / 10;
		this.vehiclesOnStreet = new Vector<Vehicle>();
	}
	public Street(int j) {
		this.speedLimit = j;
		this.priority = this.speedLimit / 10;
		this.vehiclesOnStreet = new Vector<Vehicle>();
	}
	
	public int getPriority(){
		return priority;
	}

	public void initStreet(Point a, Point b) {
		this.start = a;
		this.end = b;
	}

	public Point getStart() {
		return this.start;
	}

	public Point getEnd() {
		return this.end;
	}
	public double getLength(){
		Vector2d start = new Vector2d(this.getStart().x, this.getStart().y);
		start.sub(new Vector2d(this.getEnd().x, this.getEnd().y));
		return start.length();
	}

	public void calcDirection() {
		Vector2d start = new Vector2d(this.start.x, this.start.y);
		this.direction = new Vector2d(this.end.x, this.end.y);
		this.direction.sub(start);
		this.direction.normalize();
	}

	public Vector2d getDirection() {
		this.direction.normalize();
		return this.direction;
	}

	public void enterringStreet(Vehicle v) {
		vehiclesOnStreet.add(v);
	}

	public void leavingStreet(Vehicle v) {
		vehiclesOnStreet.remove(v);
	}

	public Vector<Vehicle> getVehicles() {
		return this.vehiclesOnStreet;
	}

	/**
	 * @param v
	 *            current vehicle
	 * @return next Vehicle on Lane, if exists else returns null
	 */
	public Vehicle getNextVehicle(Vehicle v) {
//		try{
//		Iterator<Vehicle> i = vehiclesOnStreet.iterator();
//		Vehicle tempV = i.next();
//		Vehicle prev = null;
//		while (tempV != v) {
//			prev = tempV;
//			tempV = i.next();
//		}
//
//		return prev;
//		}catch (NoSuchElementException ex){
//			return null;
//		}
		for(int i=0; i<vehiclesOnStreet.size(); i++){
			try{
				Vehicle veh = vehiclesOnStreet.get(i);
			if (v == veh){				
					return vehiclesOnStreet.get(i-1);
				}
			}catch (ArrayIndexOutOfBoundsException ex){
				return null;
			}
		}
		return null;
	}
	public Vehicle getLastVehicle(){
		return this.vehiclesOnStreet.lastElement();
	}

	public int getSpeedLimit() {
		return this.speedLimit;
	}

	public String toString() {
		return "ID: "+ id + " Speed: " + Integer.toString(speedLimit);
	}

	public void isNotOneWay() {
		this.oneWay = false;

	}

	public boolean isOneWay() {
		return this.oneWay;
	}
	
		public void placeTrafficLight(TrafficLight tl) {
		this.tl = tl;
		this.TrafficlightPosition = truncateStreet(this, 15).getEnd();
	}
	public Street truncateStreet(Street s, int cut) {
		Vector2d dir = new Vector2d(s.getEnd().x, s.getEnd().y);
		dir.sub(new Vector2d(s.getStart().x, s.getStart().y));
		dir.normalize();
		dir.scale(cut);
		Street temp = new Street(1, "test");
		Vector2d start = new Vector2d(s.getStart().x, s.getStart().y);
		Vector2d end = new Vector2d(s.getEnd().x, s.getEnd().y);
		start.add(dir);
		end.sub(dir);
		temp.initStreet(new Point((int) start.x, (int) start.y), new Point(
				(int) end.x, (int) end.y));
		return temp;
	}
	public TrafficLight getTrafficLight(){
		return this.tl;
	}
	public Point2D getTrafficLightPosition(){
		return this.TrafficlightPosition;
	}
	
	public boolean existingTL(){
		if(tl !=null){
			return true;
		}
		else{
			return false;
		}
	}
	
	public boolean equals(Street s){
		return (this.start.getX() == s.start.getX() && this.start.getY() == s.start.getY() && 
				this.end.getX() == s.end.getX() && this.end.getY() == s.end.getY());
		
	}
	
}
